package vn.ikame.android.chuyenay;

import vn.ikame.android.object.Var;
import vn.ikame.android.plugin.BaseActivity;
import vn.ikame.android.plugin.ChatActivity;
import vn.ikame.android.utils.CRandom;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Answer extends BaseActivity{
	
	private AlertDialog.Builder alert;
	public ViewPager mViewPager;
	public AnswerPagerAdapter pagerAdapter;
	public AdView adView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer);
        
        LinearLayout lLinLayout = (LinearLayout) findViewById(R.id.mainlayout2);
		RelativeLayout.LayoutParams adParams2 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		adParams2.addRule(RelativeLayout.ALIGN_BOTTOM);
		adParams2.addRule(RelativeLayout.CENTER_IN_PARENT);

		adView = new AdView(this, AdSize.SMART_BANNER, "a15058b26fd002b");
		adView.loadAd(new AdRequest());
		lLinLayout.addView(adView, adParams2);
        
        mViewPager = (ViewPager) super.findViewById(R.id.awesomepager);
        pagerAdapter = new AnswerPagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setCurrentItem(Var.itemID);
        
        LinearLayout ll = (LinearLayout) findViewById(R.id.title);
        switch (Var.cateID) {
		case Var.CHUYEN_AY:
			ll.setBackgroundResource(R.drawable.titlephongdoi);
			((LinearLayout)findViewById(R.id.content)).setBackgroundResource(R.drawable.bgphong);
			break;
		case Var.CHUYEN_ADAM:
			ll.setBackgroundResource(R.drawable.titlephongnam);
			((LinearLayout)findViewById(R.id.content)).setBackgroundResource(R.drawable.bgnam);
			break;
		case Var.CHUYEN_EVA:
			ll.setBackgroundResource(R.drawable.titlephongnu);
			((LinearLayout)findViewById(R.id.content)).setBackgroundResource(R.drawable.bgnu);
			break;
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu_question, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.chat:
//			CategoryList.this.finish();
			startActivity(new Intent(Answer.this, ChatActivity.class));
			break;
		case R.id.moregame:
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("market://search?q=ikame"));
			startActivity(intent);
			break;
		case R.id.rateapp:
			Intent intent1 = new Intent(Intent.ACTION_VIEW);
			intent1.setData(Uri.parse("market://details?id=" + Var.APP_PACKAGE));
			startActivity(intent1);
			break;
		case R.id.share:
			
			Intent sharingIntent = new Intent(Intent.ACTION_SEND);
			sharingIntent.setType("text/plain");
			sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id="  + Var.APP_PACKAGE);
			sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Chuyện 'ấy'");
			startActivity(Intent.createChooser(sharingIntent, "Share using"));
			break;
		case R.id.about:
			createAboutMessage();
			break;

		}
		return true;
	}
	
	public void createAboutMessage() {
		alert = new AlertDialog.Builder(this);
		alert.setMessage(R.string.about);
		alert.setPositiveButton("Góp ý",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						final Intent emailIntent = new Intent(
								android.content.Intent.ACTION_SEND);

						/* Fill it with Data */
						emailIntent.setType("plain/text");
						emailIntent.putExtra(
								android.content.Intent.EXTRA_EMAIL,
								new String[] { "support@ikame.vn" });
						emailIntent.putExtra(
								android.content.Intent.EXTRA_SUBJECT,
								"Ứng dụng - Chuyện ấy");
						emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
								"Nội dung: ");

						/* Send it off to the Activity-Chooser */
						startActivity(Intent.createChooser(emailIntent,
								"Send mail..."));
					}
				});

		alert.setNegativeButton("Đóng", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		alert.setCancelable(false);
		alert.show();
	}
	
	public class AnswerPagerAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			switch (Var.cateID) {
			case Var.CHUYEN_ADAM:
				return Var.listChuyenAdam.size();
			case Var.CHUYEN_EVA:
				return Var.listChuyenEva.size();
			case Var.CHUYEN_AY:
				return Var.listChuyenAy.size();
			}
			return 0;
		}

	    /**
	     * Create the page for the given position.  The adapter is responsible
	     * for adding the view to the container given here, although it only
	     * must ensure this is done by the time it returns from
	     * {@link #finishUpdate()}.
	     *
	     * @param container The containing View in which the page will be shown.
	     * @param position The page position to be instantiated.
	     * @return Returns an Object representing the new page.  This does not
	     * need to be a View, but can be some other container of the page.
	     */
		@Override
		public Object instantiateItem(View collection, int position) {
			View view;
			LayoutInflater vi = (LayoutInflater) Answer.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.pager_answer, null);
			
			//Set ảnh minh họa
			ImageView imaView = (ImageView) view.findViewById(R.id.answer_anhminhhoa);
			TextView cauhoi, cautraloi;
			cauhoi = (TextView) view.findViewById(R.id.answer_cauhoi);
			cautraloi = (TextView) view.findViewById(R.id.answer_text);
			
			cauhoi.setTypeface(Var.tf);
			cautraloi.setTypeface(Var.tf);
			
			if(Var.cateID == Var.CHUYEN_AY){
				cauhoi.setText("Hỏi: " + Var.listChuyenAy.get(position).getQuestion());
				cautraloi.setText("Trả lời: \n\n" + Var.listChuyenAy.get(position).getContent());
			} else if(Var.cateID == Var.CHUYEN_ADAM){
				cauhoi.setText("Hỏi: " + Var.listChuyenAdam.get(position).getQuestion());
				cautraloi.setText("Trả lời: \n\n" + Var.listChuyenAdam.get(position).getContent());
			} else if(Var.cateID == Var.CHUYEN_EVA){
				cauhoi.setText("Hỏi: " + Var.listChuyenEva.get(position).getQuestion());
				cautraloi.setText("Trả lời: \n\n" + Var.listChuyenEva.get(position).getContent());
			}
			
			
			int temp = CRandom.getRandom(0, 9);
			switch (Var.cateID) {
			case Var.CHUYEN_AY:
				switch (temp) {
				case 0:	imaView.setImageResource(R.drawable.doi1); break;
				case 1:	imaView.setImageResource(R.drawable.doi2); break;
				case 2:	imaView.setImageResource(R.drawable.doi3); break;
				case 3:	imaView.setImageResource(R.drawable.doi4); break;
				case 4:	imaView.setImageResource(R.drawable.doi5); break;
				case 5:	imaView.setImageResource(R.drawable.doi6); break;
				case 6:	imaView.setImageResource(R.drawable.doi7); break;
				case 7:	imaView.setImageResource(R.drawable.doi8); break;
				case 8:	imaView.setImageResource(R.drawable.doi9); break;
				case 9:	imaView.setImageResource(R.drawable.doi10); break;
				}
				break;
			case Var.CHUYEN_ADAM:
				switch (temp) {
				case 0:	imaView.setImageResource(R.drawable.nam1); break;
				case 1:	imaView.setImageResource(R.drawable.nam2); break;
				case 2:	imaView.setImageResource(R.drawable.nam3); break;
				case 3:	imaView.setImageResource(R.drawable.nam4); break;
				case 4:	imaView.setImageResource(R.drawable.nam5); break;
				case 5:	imaView.setImageResource(R.drawable.nam6); break;
				case 6:	imaView.setImageResource(R.drawable.nam7); break;
				case 7:	imaView.setImageResource(R.drawable.nam8); break;
				case 8:	imaView.setImageResource(R.drawable.nam9); break;
				case 9:	imaView.setImageResource(R.drawable.nam10); break;
				}
				break;
			case Var.CHUYEN_EVA:
				switch (temp) {
				case 0:	imaView.setImageResource(R.drawable.nu1); break;
				case 1:	imaView.setImageResource(R.drawable.nu2); break;
				case 2:	imaView.setImageResource(R.drawable.nu3); break;
				case 3:	imaView.setImageResource(R.drawable.nu4); break;
				case 4:	imaView.setImageResource(R.drawable.nu5); break;
				case 5:	imaView.setImageResource(R.drawable.nu6); break;
				case 6:	imaView.setImageResource(R.drawable.nu7); break;
				case 7:	imaView.setImageResource(R.drawable.nu8); break;
				case 8:	imaView.setImageResource(R.drawable.nu9); break;
				case 9:	imaView.setImageResource(R.drawable.nu10); break;
				}
				break;
			}
			
			
			((ViewPager) collection).addView(view,0);
			
			return view;
		}

	    /**
	     * Remove a page for the given position.  The adapter is responsible
	     * for removing the view from its container, although it only must ensure
	     * this is done by the time it returns from {@link #finishUpdate()}.
	     *
	     * @param container The containing View from which the page will be removed.
	     * @param position The page position to be removed.
	     * @param object The same object that was returned by
	     * {@link #instantiateItem(View, int)}.
	     */
		@Override
		public void destroyItem(View collection, int position, Object view) {
			((ViewPager) collection).removeView((View) view);
		}

		
		
		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view==((View)object);
		}

		
	    /**
	     * Called when the a change in the shown pages has been completed.  At this
	     * point you must ensure that all of the pages have actually been added or
	     * removed from the container as appropriate.
	     * @param container The containing View which is displaying this adapter's
	     * page views.
	     */
		@Override
		public void finishUpdate(View arg0) {
		}
		

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {}
    	
    }
}
